<?php

class Crud_Form_Decorator_Errors extends Zend_Form_Decorator_Errors
{
	protected $_translated = array(
		'isEmpty' => 'Поле обязательно для заполнения',
	);
	
    /**
     * Render errors
     *
     * @param  string $content
     * @return string
     */
    public function render($content)
    {
        $element = $this->getElement();
        $view    = $element->getView();
        if (null === $view) {
            return $content;
        }

        $errors = $element->getMessages();
        foreach ($errors as $key => $error) {
        	if (is_array($error)) {
        		unset($errors[$key]);
        	}

        	if ($element instanceof Zend_Form) {
        		if (is_array($error)) {
	        		foreach ($error as $k => $v) {
	        			$label = $element->getElement($key)->getLabel();
	        			$v = $this->_translate($k, $v);
	        			$errors[$key][$k] = "$label -- $v";
	        		}
        		}
        	} else {
        		$errors[$key] = $this->_translate($key, $error);
        	}
        }
        
        if (empty($errors)) {
            return $content;
        }

        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $errors    = $view->formErrors($errors, $this->getOptions());
        
        switch ($placement) {
            case self::APPEND:
                return $content . $separator . $errors;
            case self::PREPEND:
                return $errors . $separator . $content;
        }
    }
    
    /**
     * Translate some form errrors.
     * 
     * @param string $key
     * @param string $error
     * @return string
     */
    protected function _translate($key, $error)
    {
    	if (isset($this->_translated[$key])) {
    		return $this->_translated[$key];
    	}
    	
    	return $error;
    }
	
}